package cn.allspark.controller;

import cn.allspark.entity.WebResp;
import cn.allspark.entity.po.UserPO;
import cn.allspark.entity.po.UserRequest;
import cn.allspark.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("用户接口")
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加用户")
    public WebResp<Object> add(UserRequest user){
        if (user == null)
            return WebResp.fail("用户信息不能为空");
        int i = userService.addUser(new UserPO(user));
        if (i > 0)
            return WebResp.success();
        return WebResp.fail("添加失败,请检查参数");
    }

    /**
     * 根据手机号和QQ号查询用户
     * @param phoneNumber
     * @param qqNumber
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("根据手机号和QQ号查询用户")
    public WebResp<Object> search(String phoneNumber, String qqNumber){
        if(phoneNumber == null || qqNumber == null)
            return WebResp.fail("手机号和QQ号不能为空");
        UserPO user = userService.searchUser(phoneNumber, qqNumber);
        if(user == null)
            return WebResp.fail("未查询到该用户,请检查参数");
        return WebResp.success(user);
    }
}
